package com.example.yc23.demo.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.yc23.demo.mapper.ReviewMapper;
import com.example.yc23.demo.pojo.Review;
import com.example.yc23.demo.pojo.User;
import com.example.yc23.demo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;

@Service
public class ReviewService implements IService<Review> {
    @Autowired
    private ReviewMapper reviewMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public BaseMapper<Review> getBaseMapper() {
        return reviewMapper;
    }
    public List<Review> list(Integer state){
        return reviewMapper.selectReviewList(state);
    }
    public JsonResult thumbsUp(Integer reviewId,String token){
        JsonResult jsonResult=new JsonResult();
        if (token==null){
            jsonResult.setResponseCode(403);
            jsonResult.setResponseMsg("未登录无法评论");
        }else{
            Object obj=redisTemplate.opsForValue().get(token);
            if (obj==null){
                jsonResult.setResponseCode(403);
                jsonResult.setResponseMsg("令牌无效或已过期");
            }else{
                User currentUser=(User) obj;//成功的从redis中获得登录用户信息
                if (reviewMapper.selectthumbsup(currentUser.getId(),reviewId)==0){
                    reviewMapper.insertthumbsup(currentUser.getId(),reviewId);
                    jsonResult.setResponseMsg("点赞成功");
                }else{
                    reviewMapper.deletethumbsup(currentUser.getId(),reviewId);
                    jsonResult.setResponseMsg("点赞取消");
                }
                jsonResult.setResponseCode(200);
            }
        }
        return jsonResult;
    }

    //根据评论时间进行逆序排序
    public List<Review> orderByTime(){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("state",1);//查询状态为1 的评论
        wrapper.orderByDesc("create_time");//根据create_time字段逆序排序
        return list(wrapper);
    }

    public List<Review> orderByThumbsup(){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("state",1);//查询状态为1 的评论
        List<Review> reviewList=list(wrapper);
        Collections.sort(reviewList);//对评论列表进行排序
        return reviewList;
    }

    @Override
    public boolean saveBatch(Collection<Review> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Review> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Review> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Review entity) {
        return false;
    }

    @Override
    public Review getOne(Wrapper<Review> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Review> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<Review> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public Class<Review> getEntityClass() {
        return null;
    }
}
